package zhongbo.java.air.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.mapper.AirAlertLogMapper;
import zhongbo.java.air.model.AlertLog;
import zhongbo.java.air.service.lAirAlertLogService;

import java.util.Date;
import java.util.List;

/**
 * @ClassName AirAlertLogServiceImpl
 * @Description TODO
 * @Author 红
 * @Date 2025/4/9 10:08
 */
@Service

public class AirAlertLogServiceImpl implements lAirAlertLogService {
    @Autowired
    private AirAlertLogMapper airAlertLogMapper;

    @Override
    public AlertLog add(AlertLog airAlertLog) {
        ValidAirAlertLog(airAlertLog);
        String Id = airAlertLog.getId();
        if (StrUtil.isBlank(Id)) {
            throw new RuntimeException("id不能为空");
        }
        airAlertLog.setCreateTime(new Date());
        airAlertLog.setUpdateTime(new Date());
        int insert = airAlertLogMapper.insert(airAlertLog);
        if (insert > 0) {
            return airAlertLog;
        }
        throw new RuntimeException("添加错误");
    }

    private void ValidAirAlertLog(AlertLog airAlertLog) {
        String Icao = airAlertLog.getIcaoCode();
        if (StrUtil.isBlank(Icao)) {
            throw new RuntimeException("Icao码不能为空");
        }
        List<AlertLog> alertLogs = airAlertLogMapper.selectList(null);
        for (AlertLog alertLog : alertLogs) {
            alertLog.getIcaoCode().equals(alertLog.getIcaoCode());
            throw new RuntimeException("Icao码不能重复");
        }
    }

    @Override
    public String delete(String id) {
        if (StrUtil.isBlank(id)) {
            throw new RuntimeException("id为空");
        }
        int delete = airAlertLogMapper.deleteById(id);
        if (delete > 0) {
            return id;
        }
        throw new RuntimeException("删除失败");
    }

    @Override
    public AlertLog edit(AlertLog edit) {
        ValidAirAlertLog(edit);
        String cz = String.valueOf(findId(edit.getId()));
        if (StrUtil.isBlank(cz)) {
            throw new RuntimeException("不存在");
        }
        edit.setCreateTime(new Date());
        edit.setUpdateTime(new Date());
        int update = airAlertLogMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        throw new RuntimeException("修改失败");
    }

    @Override
    public AlertLog findId(String id) {
        if (StrUtil.isBlank(id)) {
            throw new RuntimeException("id不能为空");
        }
        return airAlertLogMapper.selectById(id);
    }

    @Override
    public PageVo<AlertLog> pageVo(Integer page, Integer pageSize
            , String icao, String startDate, String endDate
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AlertLog> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        if (StrUtil.isBlank(icao)) {
            queryWrapper.eq("icao_code", icao);
        }
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            if (Boolean.TRUE.equals(orderByAsc)) {
                // 结果正序排列
                queryWrapper.orderByAsc(oderByField);
            } else {
                // 结果倒序排列
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<AlertLog> pageParam = new Page<>(page, pageSize);
        Page<AlertLog> userPage = airAlertLogMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(userPage);
    }
}
